const koa = require('koa'),
	app = new koa(),
	mongoose = require('mongoose'),
	{ connect, initSchemas } = require('./database/init'),
	Router = require('koa-router'),
	bodyParser = require('koa-bodyparser'),
	cors = require('koa2-cors')

app.use(bodyParser())
app.use(cors())

let user = require('./appApi/User'),
	good = require('./appApi/Good'),
	category = require('./appApi/Category'),
	subCategory = require('./appApi/SubCategory')

// app.use(async ctx => {
// 	ctx.body = '<h1>Hello Koa2</h1>'
// })

;(async () => {
	await connect()
	initSchemas()
	// const User = mongoose.model('User')
	// let newUser = new User({ UserName: 'Stan', password: '123456' })
	// newUser
	// 	.save()
	// 	.then(() => {
	// 		console.log('Insert successfully')
	// 	})
	// 	.catch(err => console.log(err))
})()

let router = new Router()
router.use('/user', user.routes())
router.use('/good', good.routes())
router.use('/cate', category.routes())
router.use('/cate', subCategory.routes())

app.use(router.routes())
app.use(router.allowedMethods())

app.listen(3000, () => console.log('[Server] Starting at port 3000'))
